﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace CleanSVG
{
    public partial class CleanSVG : Form
    {
        #region Delegates

        public delegate void StatusMessager(string format, params object[] args);

        #endregion
       

        private readonly SVGCleaner _cleaner;
        private readonly SVGCleanOptions _options;

        public CleanSVG()
        {
            InitializeComponent();
            _options = SVGCleanOptions.Load();
            _cleaner = new SVGCleaner(StatusMessage);
        }

        private void StatusMessage(string format, params object[] args)
        {
            statusText.AppendText(string.Format(format, args));
            statusText.AppendText(Environment.NewLine);
            statusText.SelectionLength = 0;
            statusText.SelectionStart = statusText.TextLength;
            statusText.ScrollToCaret();
            statusText.Update();
        }

        private void selectFiles()
        {
            dialogChooseFile.Multiselect = true;
            if (dialogChooseFile.ShowDialog() == DialogResult.OK)
            {
                //Thread findWindowThread = new Thread(new ThreadStart(suppressVisioDialog));
                //findWindowThread.Start();
                List<string> svgsToClean = null;
                string[] fileList = dialogChooseFile.FileNames;
                for (int i = 0; i < fileList.Length; i++)
                {

                    string filePath = Path.GetFullPath(fileList[i]);
                    if (Path.GetExtension(filePath) == ".vsd")
                    {
                        //try
                        //{
                        //    var vc = new VisioController(StatusMessage);
                        //    svgsToClean = vc.ConvertVsdToSvgs(filePath, _options.SaveReferenceImageFromVisioFiles);
                        //}
                        //catch (FileNotFoundException)
                        //{
                        //    statusText.AppendText("Error attempting to process Visio file.\nMake sure you have Visio 2010 installed on this machine." + Environment.NewLine +
                        //                          Environment.NewLine);
                        //}
                    }
                    else
                    {
                        svgsToClean = new List<string> { filePath };
                    }

                    if (svgsToClean != null)
                    {
                        foreach (var svg in svgsToClean)
                            _cleaner.Clean(svg, _options);
                    }
                }

            }
         
        }

        private void setOptions()
        {
            var of = new OptionsForm(_options);
            if (of.ShowDialog(this) == DialogResult.OK)
                of.SaveOptions(_options);
        }

        private void CleanSVG_FormClosing(object sender, FormClosingEventArgs e)
        {
            _options.Save();
        }


        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setOptions();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            selectFiles();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About frmAbout = new About();
            frmAbout.ShowDialog();
        }


    }
}